home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 2 / Atari Mega Archive CD - Volume 2.iso / minix / up1510b.tgz / up1510b / src / fs / pipe.c < prev    next >
C/C++ Source or Header  |  1990-07-23  |  9KB  |  264 lines

  1. /* This file deals with the suspension and revival of processes.  A process can
  2.  * be suspended because it wants to read or write from a pipe and can't, or
  3.  * because it wants to read or write from a special file and can't.  When a
  4.  * process can't continue it is suspended, and revived later when it is able
  5.  * to continue.
  6.  *
  7.  * The entry points into this file are
  8.  *   do_pipe:      perform the PIPE system call
  9.  *   pipe_check:  check to see that a read or write on a pipe is feasible now
  10.  *   suspend:      suspend a process that cannot do a requested read or write
  11.  *   release:      check to see if a suspended process can be released and do it
  12.  *   revive:      mark a suspended process as able to run again
  13.  *   do_unpause:  a signal has been sent to a process; see if it suspended
  14.  */
  15.  
  16. #include "fs.h"
  17. #include <fcntl.h>
  18. #include <signal.h>
  19. #include <minix/callnr.h>
  20. #include <minix/com.h>
  21. #include "dev.h"
  22. #include "file.h"
  23. #include "fproc.h"
  24. #include "inode.h"
  25. #include "param.h"
  26.  
  27. PRIVATE message mess;
  28.  
  29. /*===========================================================================*
  30.  *                do_pipe                         *
  31.  *===========================================================================*/
  32. PUBLIC int do_pipe()
  33. {
  34. /* Perform the pipe(fil_des) system call. */
  35.  
  36.   register struct fproc *rfp;
  37.   register struct inode *rip;
  38.   int r;
  39.   dev_t device;
  40.   struct filp *fil_ptr0, *fil_ptr1;
  41.   int fil_des[2];        /* reply goes here */
  42.  
  43.   /* Acquire two file descriptors. */
  44.   rfp = fp;
  45.   if ( (r = get_fd(0, R_BIT, &fil_des[0], &fil_ptr0)) != OK) return(r);
  46.   rfp->fp_filp[fil_des[0]] = fil_ptr0;
  47.   fil_ptr0->filp_count = 1;
  48.   if ( (r = get_fd(0, W_BIT, &fil_des[1], &fil_ptr1)) != OK) {
  49.     rfp->fp_filp[fil_des[0]] = NIL_FILP;
  50.     fil_ptr0->filp_count = 0;
  51.     return(r);
  52.   }
  53.   rfp->fp_filp[fil_des[1]] = fil_ptr1;
  54.   fil_ptr1->filp_count = 1;
  55.  
  56.   /* Make the inode in the current working directory. */
  57.   device = rfp->fp_workdir->i_dev;    /* inode dev is same as working dir */
  58.   if ( (rip = alloc_inode(device, I_REGULAR)) == NIL_INODE) {
  59.     rfp->fp_filp[fil_des[0]] = NIL_FILP;
  60.     fil_ptr0->filp_count = 0;
  61.     rfp->fp_filp[fil_des[1]] = NIL_FILP;
  62.     fil_ptr1->filp_count = 0;
  63.     return(err_code);
  64.   }
  65.  
  66.   rip->i_pipe = I_PIPE;
  67.   fil_ptr0->filp_ino = rip;
  68.   dup_inode(rip);        /* for double usage */
  69.   fil_ptr1->filp_ino = rip;
  70.   rw_inode(rip, WRITING);    /* mark inode as allocated */
  71.   reply_i1 = fil_des[0];
  72.   reply_i2 = fil_des[1];
  73.   return(OK);
  74. }
  75.  
  76.  
  77. /*===========================================================================*
  78.  *                pipe_check                     *
  79.  *===========================================================================*/
  80. PUBLIC int pipe_check(rip, rw_flag, oflags, bytes, position)
  81. register struct inode *rip;    /* the inode of the pipe */
  82. int rw_flag;            /* READING or WRITING */
  83. int oflags;            /* flags set by open or fcntl */
  84. register int bytes;        /* bytes to be read or written (all chunks) */
  85. register off_t position;    /* current file position */
  86. {
  87. /* Pipes are a little different.  If a process reads from an empty pipe for
  88.  * which a writer still exists, suspend the reader.  If the pipe is empty
  89.  * and there is no writer, return 0 bytes.  If a process is writing to a
  90.  * pipe and no one is reading from it, give a broken pipe error.
  91.  */
  92.  
  93.   int r = 0;
  94.  
  95.   /* If reading, check for empty pipe. */
  96.   if (rw_flag == READING) {
  97.     if (position >= rip->i_size) {
  98.         /* Process is reading from an empty pipe. */
  99.         if (find_filp(rip, W_BIT) != NIL_FILP) {
  100.             /* Writer exists */
  101.             if (oflags & O_NONBLOCK) r = EAGAIN;
  102.             else suspend(XPIPE);    /* block reader */
  103.  
  104.             /* If need be, activate sleeping writers. */
  105.             if (susp_count > 0) release(rip, WRITE, susp_count);
  106.         }
  107.         return(r);
  108.     }
  109.   } else {
  110.     /* Process is writing to a pipe. */
  111.     if (bytes > PIPE_SIZE) return(EFBIG);
  112.     if (find_filp(rip, R_BIT) == NIL_FILP) {
  113.         /* Tell kernel to generate a SIGPIPE signal. */
  114.         sys_kill((int)(fp - fproc), SIGPIPE);
  115.         return(EPIPE);
  116.     }
  117.  
  118.     if (position + bytes > PIPE_SIZE) {
  119.         if (oflags & O_NONBLOCK) return(EAGAIN);
  120.         suspend(XPIPE);    /* stop writer -- pipe full */
  121.         return(0);
  122.     }
  123.  
  124.     /* Writing to an empty pipe.  Search for suspended reader. */
  125.     if (position == 0) release(rip, READ, susp_count);
  126.   }
  127.  
  128.   return(1);
  129. }
  130.  
  131.  
  132. /*===========================================================================*
  133.  *                suspend                         *
  134.  *===========================================================================*/
  135. PUBLIC void suspend(task)
  136. int task;            /* who is proc waiting for? (PIPE = pipe) */
  137. {
  138. /* Take measures to suspend the processing of the present system call.
  139.  * Store the parameters to be used upon resuming in the process table.
  140.  * (Actually they are not used when a process is waiting for an I/O device,
  141.  * but they are needed for pipes, and it is not worth making the distinction.)
  142.  */
  143.  
  144.   if (task == XPIPE || task == XOPEN) susp_count++;/* count procs suspended on pipe */
  145.   fp->fp_suspended = SUSPENDED;
  146.   fp->fp_fd = fd << 8 | fs_call;
  147.   fp->fp_buffer = buffer;
  148.   fp->fp_nbytes = nbytes;
  149.   fp->fp_task = -task;
  150.   dont_reply = TRUE;        /* do not send caller a reply message now */
  151. }
  152.  
  153.  
  154. /*===========================================================================*
  155.  *                release                         *
  156.  *===========================================================================*/
  157. PUBLIC void release(ip, call_nr, count)
  158. register struct inode *ip;    /* inode of pipe */
  159. int call_nr;            /* READ or WRITE */
  160. int count;            /* max number of processes to release */
  161. {
  162. /* Check to see if any process is hanging on the pipe whose inode is in 'ip'.
  163.  * If one is, and it was trying to perform the call indicated by 'call_nr'
  164.  * (READ or WRITE), release it.
  165.  */
  166.  
  167.   register struct fproc *rp;
  168.  
  169.   /* Search the proc table. */
  170.   for (rp = &fproc[0]; rp < &fproc[NR_PROCS]; rp++) {
  171.     if (rp->fp_suspended == SUSPENDED &&
  172.             rp->fp_revived == NOT_REVIVING &&
  173.             (rp->fp_fd & BYTE) == call_nr &&
  174.             rp->fp_filp[rp->fp_fd>>8]->filp_ino == ip) {
  175.         revive((int)(rp - fproc), 0);
  176.         susp_count--;    /* keep track of who is suspended */
  177.         if (--count == 0) return;
  178.     }
  179.   }
  180. }
  181.  
  182.  
  183. /*===========================================================================*
  184.  *                revive                         *
  185.  *===========================================================================*/
  186. PUBLIC void revive(proc_nr, bytes)
  187. int proc_nr;            /* process to revive */
  188. int bytes;            /* if hanging on task, how many bytes read */
  189. {
  190. /* Revive a previously blocked process. When a process hangs on tty, this
  191.  * is the way it is eventually released.
  192.  */
  193.  
  194.   register struct fproc *rfp;
  195.   register int task;
  196.  
  197.   if (proc_nr < 0 || proc_nr >= NR_PROCS) panic("revive err", proc_nr);
  198.   rfp = &fproc[proc_nr];
  199.   if (rfp->fp_suspended == NOT_SUSPENDED || rfp->fp_revived == REVIVING)return;
  200.  
  201.   /* The 'reviving' flag only applies to pipes.  Processes waiting for TTY get
  202.    * a message right away.  The revival process is different for TTY and pipes.
  203.    * For TTY revival, the work is already done, for pipes it is not: the proc
  204.    * must be restarted so it can try again.
  205.    */
  206.   task = -rfp->fp_task;
  207.   if (task == XPIPE) {
  208.     /* Revive a process suspended on a pipe. */
  209.     rfp->fp_revived = REVIVING;
  210.     reviving++;        /* process was waiting on pipe */
  211.   } else {
  212.     rfp->fp_suspended = NOT_SUSPENDED;
  213.     if (task == XOPEN) /* process blocked in open or create */
  214.         reply(proc_nr, rfp->fp_fd>>8);
  215.     else {
  216.         /* Revive a process suspended on TTY or other device. */
  217.         rfp->fp_nbytes = bytes;    /* pretend it only wants what there is */
  218.         reply(proc_nr, bytes);    /* unblock the process */
  219.     }
  220.   }
  221. }
  222.  
  223.  
  224. /*===========================================================================*
  225.  *                do_unpause                     *
  226.  *===========================================================================*/
  227. PUBLIC int do_unpause()
  228. {
  229. /* A signal has been sent to a user who is paused on the file system.
  230.  * Abort the system call with the EINTR error message.
  231.  */
  232.  
  233.   register struct fproc *rfp;
  234.   int proc_nr, task, fild;
  235.   struct filp *f;
  236.   dev_t dev;
  237.  
  238.   if (who > MM_PROC_NR) return(EPERM);
  239.   proc_nr = pro;
  240.   if (proc_nr < 0 || proc_nr >= NR_PROCS) panic("unpause err 1", proc_nr);
  241.   rfp = &fproc[proc_nr];
  242.   if (rfp->fp_suspended == NOT_SUSPENDED) return(OK);
  243.   task = -rfp->fp_task;
  244.  
  245.   if (task != XPIPE) {
  246.     if (task != XOPEN) {
  247.         fild = (rfp->fp_fd